package com.yanqu.road.dao.impl.unioncampwar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.unioncampwar.UnionCampWarUserData;
import com.yanqu.road.entity.unioncampwar.UserUnionCampWarData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class UserUnionCampWarDaoImpl extends TempDataObjectDao<UserUnionCampWarData> {

    public static String getUserUnionCampWarDataSql = "select * from `t_u_union_camp_war` where `userId`=?";
    public UserUnionCampWarData getUserUnionCampWarData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserUnionCampWarDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserUnionCampWarDataSql = "update `t_u_union_camp_war` set attackTimes=?,lastAttackTime=?,patronsAttckTimesMap=?,patronsPayAttckTimesMap=?,enterTime=?,isWorship=?,leftTimes=?,recoverTime=?,weekId=?,curioTimes=? where userId=?";
    @Override
    protected boolean updateData(UserUnionCampWarData userUnionCampWarData) {
        userUnionCampWarData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userUnionCampWarData.getAttackTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, userUnionCampWarData.getLastAttackTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userUnionCampWarData.getPatronsAttckTimesMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userUnionCampWarData.getPatronsPayAttckTimesMap())));
        para.put(i++, new DbParameter(Types.BIGINT, userUnionCampWarData.getEnterTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userUnionCampWarData.isWorship()));
        para.put(i++, new DbParameter(Types.INTEGER, userUnionCampWarData.getLeftTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, userUnionCampWarData.getRecoverTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userUnionCampWarData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, userUnionCampWarData.getCurioTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, userUnionCampWarData.getUserId()));
        if (getDb().executeNoneQuery(updateUserUnionCampWarDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserUnionCampWarDataSql = "insert into `t_u_union_camp_war`(userId,attackTimes,lastAttackTime,patronsAttckTimesMap,patronsPayAttckTimesMap,enterTime,isWorship,leftTimes,recoverTime,weekId,curioTimes) values (?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserUnionCampWarData userUnionCampWarData) {
        userUnionCampWarData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userUnionCampWarData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userUnionCampWarData.getAttackTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, userUnionCampWarData.getLastAttackTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userUnionCampWarData.getPatronsAttckTimesMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userUnionCampWarData.getPatronsPayAttckTimesMap())));
        para.put(i++, new DbParameter(Types.BIGINT, userUnionCampWarData.getEnterTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userUnionCampWarData.isWorship()));
        para.put(i++, new DbParameter(Types.INTEGER, userUnionCampWarData.getLeftTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, userUnionCampWarData.getRecoverTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userUnionCampWarData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, userUnionCampWarData.getCurioTimes()));
        if (getDb().executeNoneQuery(insertUserUnionCampWarDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserUnionCampWarData parseData(ResultSet rs) throws SQLException {
        UserUnionCampWarData userUnionCampWarData = new UserUnionCampWarData();
        userUnionCampWarData.setUserId(rs.getLong("userId"));
        userUnionCampWarData.setAttackTimes(rs.getInt("attackTimes"));
        userUnionCampWarData.setLastAttackTime(rs.getLong("lastAttackTime"));
        userUnionCampWarData.setPatronsAttckTimesMap(JSON.parseObject(rs.getString("patronsAttckTimesMap"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));
        String patronsPayAttckTimesMap = rs.getString("patronsPayAttckTimesMap");
        if (!StringUtils.isNullOrEmpty(patronsPayAttckTimesMap)){
            userUnionCampWarData.setPatronsPayAttckTimesMap(JSON.parseObject(patronsPayAttckTimesMap, new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));
        }
        userUnionCampWarData.setEnterTime(rs.getLong("enterTime"));
        userUnionCampWarData.setWorship(rs.getBoolean("isWorship"));
        userUnionCampWarData.setLeftTimes(rs.getInt("leftTimes"));
        userUnionCampWarData.setRecoverTime(rs.getLong("recoverTime"));
        userUnionCampWarData.setWeekId(rs.getInt("weekId"));
        userUnionCampWarData.setCurioTimes(rs.getInt("curioTimes"));
        return userUnionCampWarData;
    }
}
